package com.xjl.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xjl.demo.Admin;
import com.xjl.service.AdminService;
import com.xjl.service.impl.AdminServiceImpl;
/**
 * 处理用户新增管理员
 * @author JianLin_Xu
 *
 */
@WebServlet("/insert.do")
public class InsertAdminServlet extends HttpServlet {
	AdminService as = new AdminServiceImpl();
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//设置请求编码格式
		req.setCharacterEncoding("utf-8");
		//设置响应编码格式
		resp.setContentType("text/html;utf-8");
		//处理请求信息
		int adminid = Integer.parseInt(req.getParameter("adminid")) ;
		String adminname = req.getParameter("adminname");
		String adminpwd = req.getParameter("adminpwd");
		Admin admin = new Admin();
		admin.setAdminid(adminid);
		admin.setAdminname(adminname);
		admin.setAdminpwd(adminpwd);
		int index = as.InsertAdminService(admin);
		HttpSession session = req.getSession();
		if(index==1){
			session.setAttribute("flag", "新增管理员成功！");
		}else if(index==-1){
			session.setAttribute("flag", "账号已存在，请重新输入！");
		}else{
			session.setAttribute("flag", "网络异常，请稍后重试！");
		}
		resp.sendRedirect("/pcshop/selectall.do");//用重定向反正数据重复提交，造成数据库混乱
	}

}
